package com.example.user.controller;

import com.example.common.core.controller.BaseController;
import com.example.common.core.domain.R;
import com.example.common.core.domain.TableDataInfo;
import com.example.common.core.enums.UserIdentity;
import com.example.user.aspect.CheckUserIdentity;
import com.example.user.domain.course.dto.CourseQueryDTO;
import com.example.user.domain.course.vo.CourseDetailVO;
import com.example.user.domain.studentBuyCourse.dto.BuyCourseSaveDTO;
import com.example.user.service.ICourseService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/course")
@Tag(name = "课程管理接口")
public class CourseController extends BaseController {
    @Autowired
    private ICourseService courseService;

    /*
    * 查询这个老师的精品课程
    * common
    */
    @GetMapping("/list")
    @Operation(summary = "查询教师的精品课程")
//    @CheckUserIdentity(identity = UserIdentity.TEACHER)
    public TableDataInfo list(CourseQueryDTO courseQueryDTO) {
        return getTableDataInfo(courseService.list(courseQueryDTO));
    }

    @GetMapping("/purchasedCourseList")
    @Operation(summary = "查询已购买的课程列表")
    @CheckUserIdentity(identity = UserIdentity.STUDENT)
    public TableDataInfo purchasedCourseList(CourseQueryDTO courseQueryDTO) {
        return getTableDataInfo(courseService.purchasedCourseList(courseQueryDTO));
    }
    @GetMapping("/detail")
    @Operation(summary = "获取课程详情")
    public R<CourseDetailVO> detail(Long courseId) {
        return R.ok(courseService.detail(courseId));
    }

    @GetMapping("/courseListForSale")
    @Operation(summary = "获取可销售的课程列表")
    @CheckUserIdentity(identity = UserIdentity.STUDENT)
    public TableDataInfo courseListForSale(CourseQueryDTO courseQueryDTO) {
        return getTableDataInfo(courseService.courseListForSale(courseQueryDTO));
    }

    @PutMapping("/buyCourse")
    @Operation(summary = "购买课程")
    @CheckUserIdentity(identity = UserIdentity.STUDENT)
    public R<Void> buyCourse(BuyCourseSaveDTO buyCourseSaveDTO ) {
        return toR(courseService.buyCourse(buyCourseSaveDTO));
    }

    @GetMapping("/haveYouPurchased")
    @Operation(summary = "检查是否已购买课程")
    @CheckUserIdentity(identity = UserIdentity.STUDENT)
    public R<Boolean> haveYouPurchased(Long courseId) {
        return R.ok(courseService.haveYouPurchased(courseId));
    }

    @GetMapping("/classroomCourseList")
    @Operation(summary = "获取班级课程列表")
    public TableDataInfo classroomCourseList(CourseQueryDTO courseQueryDTO) {
        return getTableDataInfo(courseService.classroomCourseList(courseQueryDTO));
    }
    
    /**
     * 教师提交课程审核
     * @param courseId 课程ID
     * @return 操作结果
     */
    @PutMapping("/submitForReview")
    @Operation(summary = "提交课程审核")
    @CheckUserIdentity(identity = UserIdentity.TEACHER)
    public R<String> submitForReview(@RequestParam Long courseId) {
        return courseService.submitForReview(courseId);
    }
}
